import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import path from "path"
// import AutoImport from "unplugin-auto-import/vite"
// import Components from "unplugin-vue-components/vite"
// import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import DefineOptions from 'unplugin-vue-define-options/vite';

import { PRO_BASE_URL, DEV_BASE_URL, PORT } from './src/assets/common/config'

import versionUpdatePlugin from "./src/plugins/versionUpdatePlugin";

import UnoCSS from 'unocss/vite'

const isProduction = process.env.NODE_ENV === "production"

// const pagePath = window.location.host;
// console.log('当前页面路径vite:', pagePath);


// https://vitejs.dev/config/
export default defineConfig({
    base: "./", // 设置打包路径
    productionSourceMap: !isProduction, //关闭生产环境下的SourceMap映射文件
    plugins: [
        vue({
            script: {
                defineModel: true
            }
        }),
        // 
        DefineOptions(),
        // AutoImport({
        //     resolvers: [ElementPlusResolver()],
        // }),
        // Components({
        //     resolvers: [ElementPlusResolver()],
        // }),
        UnoCSS(),
        // 自定义插件，在 public 文件夹下生成版本信息 version.json
        versionUpdatePlugin({
            version: Date.now()
        })
    ],
    resolve: {
        alias: {
            // 如果报错__dirname找不到，需要安装node,执行npm install @types/node --save-dev
            "@": path.resolve(__dirname, "src"),
            "@assets": path.resolve(__dirname, "src/assets"),
            "@stores": path.resolve(__dirname, "src/stores"),
            "@mixins": path.resolve(__dirname, "src/mixins"),
            "@components": path.resolve(__dirname, "src/components"),
            "@common": path.resolve(__dirname, "src/assets/common"),
            "@images": path.resolve(__dirname, "src/assets/images"),
            "@views": path.resolve(__dirname, "src/views"),
        },
    },
    css: {
        sourceMap: !isProduction, // css sourceMap 配置
        preprocessorOptions: {
            less: {
                modifyVars: {
                    hack: `true; @import (reference) "${path.resolve("src/assets/css/base.less")}";`,
                },
                javascriptEnabled: true,
            },
        },
    },
    build: {
        outDir: "dist",
        assetsDir: "assets", //指定静态资源存放路径
        sourcemap: false, //是否构建source map 文件
        terserOptions: {
            // 生产环境移除console
            compress: {
                drop_console: true,
                drop_debugger: true,
            },
        },
    },
    server: {
        https: false, // 是否开启 https
        open: false, // 是否自动在浏览器打开
        cors: true, // 允许跨域
        host: "0.0.0.0",
        proxy: {
            "/v1": {
				// target: "https://"+pagePath+'/api/v1/', // 后台接口
                target: PRO_BASE_URL, // 后台接口
                changeOrigin: true,
                secure: false, // 如果是https接口，需要配置这个参数
                // ws: true, //websocket支持
                rewrite: (path) => path.replace(/^\/v1/, ""), // 匹配/supplier/admin  格式为：/^\/supplier\/admin/
            },
        },
    },
    // 引入第三方的配置
    optimizeDeps: {
        include: [],
    },

})
